import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function(location) {
//   return originalPush.call(this, location).catch(err => {})
// };
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.replace = function(location) {
  return originalPush.call(this, location).catch(err => {})
};


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children:[
      {
         path: '/home/searchpopup',
          component: () => import(/* webpackChunkName: "searchpopup" */ '../views/SearchPopup.vue')
      }
    ],
    meta:{
      tabbarActiveNum:0,
      isShowTabbar:true
    }
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
    meta:{
      tabbarActiveNum:1,
       isShowTabbar:true
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue'),
    meta:{
      tabbarActiveNum:2,
       isShowTabbar:true
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
    meta:{
      tabbarActiveNum:3,
       isShowTabbar:true
    }
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    meta:{
      tabbarActiveNum:4,
       isShowTabbar:true
    }
  },
  {
    path: '/productsDetail',
    name: 'ProductsDetail',
    component: () => import(/* webpackChunkName: "user" */ '../components/ProductsDetail.vue'),
    meta:{
       isShowTabbar:false
    }
  },
 
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


export default router
// 全局路由守卫   路由跳转时执行这里的代码
router.beforeEach((to,from,next)=>{
  // if(to.path=="/cart"){
  //   // 判断有没有登录
  //   // 如果没有登录，跳转到 /user 页面
  //   let token=localStorage.getItem("token");
  //   if(token){
  //     next()
  //   }else{
  //     Vue.prototype.$toast("请先登录")
  //     // 拿不到token，说明没有登录，跳转到我的页面登录
  //     setTimeout(()=>{
  //       next("/user")
  //     },800)
     
  //   }
  //    return
  // }
  next();  //必须调用next，才能跳转到想要去的页面
})